package com.myCompany.edu.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.myCompany.edu.realtime.app.BaseAppV1;
import com.myCompany.edu.realtime.bean.TrafficKeywordBean;
import com.myCompany.edu.realtime.bean.TrafficPageViewBean;
import com.myCompany.edu.realtime.common.Constant;
import com.myCompany.edu.realtime.util.AtguiguUtil;
import com.myCompany.edu.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @author
 * @date 2022/10/21 15:27
 */

public class Dws02_DwsTrafficKeywordCountWindow extends BaseAppV1 {
    public static void main(String[] args) {
        new Dws02_DwsTrafficKeywordCountWindow().init(3002,2,
                "Dws02_DwsTrafficKeywordCountWindow", Constant.TOPIC_DWD_TRAFFIC_DISPLAY);
    }

    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        SingleOutputStreamOperator<TrafficKeywordBean> beanStream = parseStream(stream);
        SingleOutputStreamOperator<TrafficKeywordBean> resultBean = windowAndAgg(beanStream);
        resultBean.print();
        writeClickHouse(resultBean);
    }

    private void writeClickHouse(SingleOutputStreamOperator<TrafficKeywordBean> resultBean) {
        resultBean.addSink(FlinkSinkUtil.getClickHouseSink("dws_traffic_keyword_count_window", TrafficKeywordBean.class));
    }

    private SingleOutputStreamOperator<TrafficKeywordBean> windowAndAgg(SingleOutputStreamOperator<TrafficKeywordBean> stream) {
         return stream
                .assignTimestampsAndWatermarks(WatermarkStrategy
                        .<TrafficKeywordBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((bean, ts) -> bean.getTs()))
                 .keyBy(bean -> bean.getKeyword())
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                 .reduce(new ReduceFunction<TrafficKeywordBean>() {
                             @Override
                             public TrafficKeywordBean reduce(TrafficKeywordBean bean1, TrafficKeywordBean bean2) throws Exception {
                                 bean1.setKeywordCt(bean1.getKeywordCt() + bean2.getKeywordCt());
                                 return bean1;

                             }
                         },
                         new ProcessWindowFunction<TrafficKeywordBean, TrafficKeywordBean, String, TimeWindow>() {
                             @Override
                             public void process(String s, Context context, Iterable<TrafficKeywordBean> iterable, Collector<TrafficKeywordBean> collector) throws Exception {
                                 TrafficKeywordBean bean = iterable.iterator().next();
                                 bean.setStt(AtguiguUtil.toDateTime(context.window().getStart()));
                                 bean.setEdt(AtguiguUtil.toDateTime(context.window().getEnd()));

                                 bean.setTs(System.currentTimeMillis());
                                 collector.collect(bean);
                             }
                         });
    }

    private SingleOutputStreamOperator<TrafficKeywordBean> parseStream(DataStreamSource<String> stream) {
         return stream
                .filter(value -> "keyword".equals(JSON.parseObject(value).getString("item_type")))
                .map(new MapFunction<String, TrafficKeywordBean>() {
                    @Override
                    public TrafficKeywordBean map(String s) throws Exception {
                        String keyword = JSON.parseObject(s).getString("item");
                        Long ts = JSON.parseObject(s).getLong("ts");
                        return new TrafficKeywordBean("", "", keyword, 1L, ts);
                    }
                });

    }
}
